<template>
  <div class="app-container">
    <div class="page-header">
      <el-button type="primary" icon="el-icon-plus" size="medium" @click="editMenu(null)">新增权限</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      class="x-l-admin-table"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="权限名称" align="left" header-align="center" />
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.type === 1 ? 'primary' : 'warning'"
            size="small"
          >
            {{ scope.row.type === 1 ? '菜单' : '按钮' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单路由" header-align="center" />
      <el-table-column prop="api_url" label="接口地址" header-align="center" />
      <el-table-column label="是否启用权限控制" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'" size="small">
            {{ scope.row.status === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.created_at | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editMenu(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            :disabled="scope.row.type === 2"
            @click="editMenu(scope.row, false)"
          >新增
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteMenu(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--添加修改-->
    <permission :show="drawerShow" :first-row="currentRow" />
  </div>
</template>

<script>
import api from '@/api'
import getJson from '@/utils/getJson'
import permission from './edit/permission'

export default {
  components: {
    permission
  },
  data() {
    return {
      list: [],
      listLoading: true,
      drawerShow: false,
      currentRow: null
    }
  },
  created() {
    this.getList()
  },
  provide() {
    return {
      app: this
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      api.getPermissionsList().then(response => {
        this.list = getJson(response)
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 显示添加框
    editMenu(row, type = true) {
      this.drawerShow = true
      if (row === null) {
        this.currentRow = {}
      } else {
        if (type === false) {
          this.currentRow = {
            'pid': row.id
          }
        } else {
          this.currentRow = row
        }
      }
    },
    deleteMenu(id) {
      this.$confirm('删除菜单会同时删除子菜单及子权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.deletePermission({ 'id': id }).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }).catch(() => {
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
